﻿using System.Linq;
using System.Web;
using System.Web.Mvc;
using GKFX.Common.Business.BOL.Common;
using Century.Model;
//using Kooboo.CMS.Sites.Models;
//using Kooboo.CMS.Sites.Services;
using Telerik.Web.Mvc;
using Century.Web.Security.Attributes
using System.Collections.Generic;
using GKFX.Common.UI.MVC;

namespace Century.Web.MVC.AdminArea
{
    //[Kooboo.CMS.Web.Authorizations.Authorization(AreaName = "AdminTools", Group = "", Name = "SiteManager")]
    [Authorization(Group = "", Name = "SiteManger")]
    public class SiteConfigurationController : CenturyAdminBaseController
    {
        //
        // GET: /AdminTools/SiteManaer/

        CountrySiteBSO db = new CountrySiteBSO();
        CommonCountryBSO countryDb = new CommonCountryBSO();
        LanguageBSO languageDb = new LanguageBSO();

        public ActionResult Index()
        {
            ViewBag.CountryList = countryDb.GetAll().Select(c => new { c.Id, c.Name }).AsEnumerable();
            ViewBag.LanguageList = languageDb.GetAll().Select(c => new { c.Id, c.Name }).AsEnumerable();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        [GridAction]
        public ActionResult EditCountrySite(int id)
        {
            CountrySite countrySite = db.Find(p => p.Id == id);
            TryUpdateModel(countrySite);
            db.Update(countrySite);
            var d = new GridModel
            {
                Data = db.GetAll().Select(c => new { c.Id, c.CountryId, c.LanguageId, c.Name, c.Path, c.IsDefault, c.IsActive, c.SiteAnalyticsScript, c.LiveApplicationUrl, c.DemoApplicationUrl, c.LoginUrl, c.SiteAddress })
            };
            return View(d);
        }

        [GridAction]
        public ActionResult GetCountrySiteList()
        {
            var d = new GridModel
             {
                 Data = db.GetAll().Select(c => new { c.Id, c.CountryId, c.LanguageId, c.Name, c.Path, c.IsDefault, c.IsActive, c.SiteAnalyticsScript, c.LiveApplicationUrl, c.DemoApplicationUrl, c.LoginUrl, c.SiteAddress })
             };
            return View(d);
        }

        #region GeneratingLabels
        //generating labels in db depens on default(EN countrySite=14) list and different conditions
        public void GenerateLabels(int Id)
        {
            //using (TranslationTextBSO translateTextDb = new TranslationTextBSO())
            //{
            //    List<TranslationText> defaultTextList = translateTextDb.GetList(c => c.CountrySiteId == 14);
            //    foreach (TranslationText item in defaultTextList)
            //    {
            //        //If key is not exist for current country site.
            //        if (!translateTextDb.Exist(c => c.TranslationKey.Equals(item.TranslationKey, System.StringComparison.OrdinalIgnoreCase)
            //            && c.CountrySiteId == Id))
            //        {
            //            //Create and insert new key for current country site.
            //            TranslationText translationText = new TranslationText
            //            {
            //                CountrySiteId = Id,
            //                TranslationKey = item.TranslationKey,
            //                TranslationValue = item.TranslationValue
            //            };
            //            translateTextDb.Insert(translationText);
            //        }
            //        //If there is dublicate key it must be delete.
            //        else if (translateTextDb.CountOfRecord(c => c.TranslationKey.Equals(item.TranslationKey, System.StringComparison.OrdinalIgnoreCase)
            //            && c.CountrySiteId == Id) > 1)
            //        {
            //            var translateList = translateTextDb.GetAll(c => c.TranslationKey.Equals(item.TranslationKey, System.StringComparison.OrdinalIgnoreCase)
            //            && c.CountrySiteId == Id).Skip(1).ToList();
            //            int count = translateList.Count();
            //            for (int i = 0; i < count; i++)
            //            {
            //                translateTextDb.Delete(c => c.Id == translateList[i].Id);
            //            }
            //        }
            //    }

            //}
            //GenerateOtherLabels(Id);
 
        }
        //generation otherlabels, which exist for othersites except EN(countrySite=14)
        //EN could not include all labels for example for localized page labels...
        //public void GenerateOtherLabels(int Id)
        //{
        //    using (TranslationTextBSO translateTextDb = new TranslationTextBSO())
        //    {
        //        List<string> defaultTextList = translateTextDb.GetList().Select(c => c.TranslationKey).Distinct().ToList<string>();
        //        foreach (string item in defaultTextList)
        //        {
        //            //If key is not exist for current country site.

        //            if (!translateTextDb.Exist(c => c.TranslationKey.Equals(item, System.StringComparison.OrdinalIgnoreCase) && c.CountrySiteId == Id))
        //            {
        //                TranslationText otherlabel = new TranslationText
        //                {
        //                    CountrySiteId = 14,
        //                    TranslationKey = item,
        //                    TranslationValue = ""

        //                };
        //                translateTextDb.Insert(otherlabel);


        //            }

        //        }
        //    }
        //}

        #endregion

    }
}
